﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using System.Text;

namespace Runda.CMS.Portal.Report
{
    public partial class SalesRpt_104 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 2012; i <= 2015; i++)
                {
                    ddlBudgetYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                DataTable dt = new BudgetBll().Companylist(base.CurrentUserID.ToString());
                ddlCompany.DataSource = dt;
                ddlCompany.DataTextField = "Departmentname";
                ddlCompany.DataValueField = "Departmentname";
                ddlCompany.DataBind();
                ddlCompany.Items.Add(new ListItem("--不限--", "0"));
                ddlCompany.SelectedValue = "0";

                

                string sqlWhere = getWhereClause();
                LoadContractTrack(sqlWhere);

            }
        }

        private void LoadContractTrack(string sqlWhere)
        {
            //set reference
            ReportViewer ReportViewer = ReportViewer1;

            //get rdlc path
            string strRootPath = this.Server.MapPath(this.Request.ApplicationPath);
            string strFilePath = strRootPath + "Report\\rdlc\\SalesRpt_104.rdlc";

            strFilePath = "report/rdlc/SalesRpt_104.rdlc";

            //set report viewer property
            ReportViewer.SizeToReportContent = true;
            ReportViewer.ZoomMode = ZoomMode.Percent;
            ReportViewer.AsyncRendering = false;
            
            //set report viewer work mode
            ReportViewer.ProcessingMode = ProcessingMode.Local;
            LocalReport lp = ReportViewer.LocalReport;
            lp.ReportPath = strFilePath;

            //get dataset  
            BillBll bll = new BillBll();
            DataSet ds;
            ds = bll.SalesRpt_104(sqlWhere, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue);

            ds.DataSetName = "ds1_dbo_vw_presales2Sale_ByProduct";
            //set data set
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "ds1_dbo_vw_presales2Sale_ByProduct";

            DataTable dt = ds.Tables[0];
            DataTable dttmp;
            DataRow dr;
            DataRow drtmp;

            dr = dt.NewRow();
            dttmp = bll.sumSalesRpt_104(sqlWhere, "", ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue);
            drtmp = dttmp.Rows[0];
            dr["FactoryName"] = "900合计：";
            dr["rdpresalesAmount"] = drtmp["rdpresalesAmount"];
            dr["rdSaleAmount"] = drtmp["rdSaleAmount"];
            
            dr["kxpresalesAmount"] = drtmp["kxpresalesAmount"];
            dr["kxSaleAmount"] = drtmp["kxSaleAmount"];
            dr["yxpresalesAmount"] = drtmp["yxpresalesAmount"];
            dr["yxSaleAmount"] = drtmp["yxSaleAmount"];
            dr["hzpresalesAmount"] = drtmp["hzpresalesAmount"];
            dr["hzSaleAmount"] = drtmp["hzSaleAmount"];
            dr["hcpresalesAmount"] = drtmp["hcpresalesAmount"];
            dr["hcSaleAmount"] = drtmp["hcSaleAmount"];
            dr["TotalpresalesAmount"] = drtmp["TotalpresalesAmount"];
            dr["TotalSaleAmount"] = drtmp["TotalSaleAmount"];
            dr["rdrate"] = drtmp["rdrate"];
            dr["kxrate"] = drtmp["kxrate"];
            dr["yxrate"] = drtmp["yxrate"];
            dr["hzrate"] = drtmp["hzrate"];
            dr["hcrate"] = drtmp["hcrate"];
            dr["totalrate"] = drtmp["totalrate"];
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dttmp = bll.sumSalesRpt_104(sqlWhere, " and isOwner='1'", ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue);
            drtmp = dttmp.Rows[0];
            dr["FactoryName"] = "901自产产品小计：";
            dr["rdpresalesAmount"] = drtmp["rdpresalesAmount"];
            dr["rdSaleAmount"] = drtmp["rdSaleAmount"];
            dr["kxpresalesAmount"] = drtmp["kxpresalesAmount"];
            dr["kxSaleAmount"] = drtmp["kxSaleAmount"];
            dr["yxpresalesAmount"] = drtmp["yxpresalesAmount"];
            dr["yxSaleAmount"] = drtmp["yxSaleAmount"];
            dr["hzpresalesAmount"] = drtmp["hzpresalesAmount"];
            dr["hzSaleAmount"] = drtmp["hzSaleAmount"];
            dr["hcpresalesAmount"] = drtmp["hcpresalesAmount"];
            dr["hcSaleAmount"] = drtmp["hcSaleAmount"];
            dr["TotalpresalesAmount"] = drtmp["TotalpresalesAmount"];
            dr["TotalSaleAmount"] = drtmp["TotalSaleAmount"];
            dr["rdrate"] = drtmp["rdrate"];
            dr["kxrate"] = drtmp["kxrate"];
            dr["yxrate"] = drtmp["yxrate"];
            dr["hzrate"] = drtmp["hzrate"];
            dr["hcrate"] = drtmp["hcrate"];
            dr["totalrate"] = drtmp["totalrate"];
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dttmp = bll.sumSalesRpt_104(sqlWhere, " and isOwner='0'", ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue);
            drtmp = dttmp.Rows[0];
            dr["FactoryName"] = "902代理产品小计：";
            dr["rdpresalesAmount"] = drtmp["rdpresalesAmount"];
            dr["rdSaleAmount"] = drtmp["rdSaleAmount"];
            dr["kxpresalesAmount"] = drtmp["kxpresalesAmount"];
            dr["kxSaleAmount"] = drtmp["kxSaleAmount"];
            dr["yxpresalesAmount"] = drtmp["yxpresalesAmount"];
            dr["yxSaleAmount"] = drtmp["yxSaleAmount"];
            dr["hzpresalesAmount"] = drtmp["hzpresalesAmount"];
            dr["hzSaleAmount"] = drtmp["hzSaleAmount"];
            dr["hcpresalesAmount"] = drtmp["hcpresalesAmount"];
            dr["hcSaleAmount"] = drtmp["hcSaleAmount"];
            dr["TotalpresalesAmount"] = drtmp["TotalpresalesAmount"];
            dr["TotalSaleAmount"] = drtmp["TotalSaleAmount"];
            dr["rdrate"] = drtmp["rdrate"];
            dr["kxrate"] = drtmp["kxrate"];
            dr["yxrate"] = drtmp["yxrate"];
            dr["hzrate"] = drtmp["hzrate"];
            dr["hcrate"] = drtmp["hcrate"];
            dr["totalrate"] = drtmp["totalrate"];
            dt.Rows.Add(dr);
            
            rds.Value = dt;
            lp.DataSources.Clear();
            lp.DataSources.Add(rds);

            ReportViewer.LocalReport.Refresh();

        }

        private string getWhereClause()
        {
            StringBuilder sb = new StringBuilder();

            //sb.Append(" and (companyname in(").Append("select DepartmentName from cms_sys_Department where iscompany = 1 and DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("'))");
            //sb.Append(" or CategoryCode in(").Append("select categoryCode from Cms_Ctrt_InstrumentCategory where PM='").Append(base.CurrentUserEntity.UserName.ToString()).Append("'))");
            sb.Append(" and (companyname in(").Append("select DepartmentName from cms_sys_Department where iscompany = 1 and DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("'))");
            sb.Append(" and CategoryCode in(").Append("select categoryCode from Cms_Ctrt_InstrumentCategory where categoryGUID in(select categoryGUID from Cms_sys_UserProductCategory where UserID ='").Append(base.CurrentUserID.ToString()).Append("')))");
                //sb.Append(" or a.[sales] ='").Append(base.CurrentUserEntity.UserName).Append("'");
                //sb.Append(" or a.[Producter] ='").Append(base.CurrentUserEntity.UserName).Append("'");
                //sb.Append(" or a.[ProductManager] ='").Append(base.CurrentUserEntity.UserName).Append("')");
            
            if (ddlCompany.SelectedValue != "0")
            {
                sb.Append(" and companyname = '" + ddlCompany.SelectedValue.ToString() + "'");
            }

            

            return sb.ToString();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string sqlWhere = getWhereClause();
            LoadContractTrack(sqlWhere);
        }

        
    }
}
